<?php
/**
 * Controller for actions (in admin area) of member module
 *  
 * @version 2012-10-10
 * @package member
 * @subpackage controller
 * @category Member_Controller_Admin
 */

class Category_Controller_Admin extends Controller
{
	/**
	 * Default action
	 *
	 * @version 2012-10-10
	 * @param Request $req
	 * @return View
     * @access public 
	 */	
	public function processIndex($req)
	{
		return $this->processList($req);
	}

	/**
	 * List
	 * This function shows the list of categories.
	 * This function forms a query to the database with the help of a block of filters and returns an object VIEW that contains the array 'members' with data
	 * 
	 * @version 2012-10-24
	 * @param Request $req
	 * @return View
	 * @access public 
	 */
	public function processList($req) {
		// Here local values are declared 
		$dbCategories = Db::getModel('categories');
		$view = $this->getView("admin_category");
		m('category');
		$url = new Url();
		$pager = new Pager($url,_cfg('global.pager'));
		$sorter = new Sorter($url);
		
		$sorterCfg = array(
			'id',
			'title',
			'status'
        );
		
		// Here filter conditions are set up
        $filterCfg= array(
            'title' => 'like',
			'status'=>'='
        );
		
		$filter = new Filter('admin_category', $filterCfg);
		$filter->process($req);
		$sorter->initUp($sorterCfg);
		$sort   = array($sorter->getOrder());
		$where = $filter->getCriteria();
		$view->setVar('fltInfo',$filter->getInfo());
		
		$rows = $dbCategories->getCategoryList(array_merge(array("status"=>"!='deleted'"),$where),$pager,$sort);
		$tpl_tr = array('id'=>'tpl_tr','class'=>'hide','status'=>'active');
		array_unshift($rows, $tpl_tr);
		$view->setVar('rows',$rows);
		
		$view->setVar('srtInfo', $sorter->getInfo());
		$view->setVar('pagerInfo',$pager->getInfo());

		return $view;
	}
	
	/**
	 * Deactivate
	 * This function edits the status of a categories in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-05-08
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processDeactivate($req){
		$url = new Url();
		$uuids = $req->getArray("uuid");
		$dbCategories = Db::getModel('categories');
		foreach($uuids as $uuid){
			$dbCategories->inactive($uuid);
		}
		$req->forward($url->getUrl(),_fmt('ok'));
	}
	
	/**
	 * Activate
	 * This function edits the status of a categories in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-05-08
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processActivate($req){
		$url = new Url();
		$uuids = $req->getArray("uuid");
		$dbCategories = Db::getModel('categories');
		foreach($uuids as $uuid){
			$dbCategories->activate($uuid);
		}
		$req->forward($url->getUrl(),_fmt('ok'));
	}
	
	/**
	 * Delete
	 * This function edits the status of a categoryies in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-05-08
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processDelete($req){
		$url = new Url();
		$uuids = $req->getArray("uuid");
		$dbCategories = Db::getModel('categories');
		foreach($uuids as $uuid){
			$dbCategories->delete($uuid);
		}
		$req->forward($url->getUrl(),_fmt('ok'));
	}
	
	/**
	 * ProjectAjaxAction
	 * This function edits the status of a category in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-05-08
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processAjaxCategoryAction($req){
		$dbCategories = Db::getModel('categories');
		$uuid = $req->get('uuid');
		$method = $req->get('method');
		
			if($method == "deactivate")
				echo $dbCategories->inactive($uuid);
			elseif($method == "activate")
				echo $dbCategories->activate($uuid);
			elseif($method == "delete")
				echo $dbCategories->delete($uuid);
		exit;
	}
	
	 /**
	 * AjaxAddProject
	 * This function add member in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-01-18
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processAjaxAddCategory($req){
		$dbCategories = Db::getModel('categories');
		$data = $req->getArray('data');
		$rules = array();
		
		$fields['data[title]'] = array('title'=>'Title category', 'def' => 'required');
		
		$val = new Validator($fields);
		if(!$val->isValid($req->getArgs())){
			$result['status'] = "error";
			foreach($val->getErrors() as $e)
				$result['msg'] .= $e."<br>";
		}
		else{
			if($data['id']){
				$category = $dbCategories->find((int)$data['id']);
				if(strtolower($category['title']) == strtolower($data['title']) or $dbCategories->countBy(array('title'=>"='{$data['title']}'")) < 1){
					$dbCategories->update(array('title'=>$data['title']),$data['id']);
					$result['title'] = $data['title'];
					$result['msg'] = _fmt("ok");
					$result['uuid'] = $data['id'];
					$result['status'] = "ok_update";
				}else{
					$result['status'] = "error";
					$result['msg'] .= _fmt("category.exists");
				}
			}else{
				if($dbCategories->countBy(array('title'=>"='{$data['title']}'")) > 0){
					$result['status'] = "error";
						$result['msg'] .= _fmt("category.exists");
				}else{
					$request = $dbCategories->getIdTitle($data['title'],true);
					$result['status'] = "ok";
					$result['uuid'] = $request['id'];
					$result['msg'] .= _fmt("ok");
					$result['title'] = $data['title'];
				}
			}
		}
		echo json_encode($result);
		exit;
	}
}